﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PCMarket
{
    /// <summary>
    /// 卖场类
    /// </summary>
    public class Market
    {

        /// <summary>
        /// 走进电脑城里面
        /// </summary>
        public void StepIn()
        {
            Console.WriteLine("------------欢迎来到XXX电脑超市------------------");
            Hello();
        }
        /// <summary>
        /// 电脑集合
        /// </summary>
        private List<Computer> pcList = new List<Computer>();
        private Dictionary<string, string> menuList = new Dictionary<string, string>();

        public Market()
        {
            menuList.Add("show", "查看所有的电脑");
            menuList.Add("buy", "购买电脑");
            menuList.Add("exit", "玩完了，回家");

            pcList.Add(new Computer("小新新", "联想", 5400));
            pcList.Add(new Computer("拯救者", "联想", 7400));
            pcList.Add(new Computer("X80", "外星人", 10400));
            pcList.Add(new Computer("Mac Pro", "Apple", 12000));

        }

        private void ShowAllPc()
        {
            Console.WriteLine("电脑编号\t型号\t品牌\t价格");
            int index = 0;
            foreach (Computer pc in pcList)
            {
                Console.WriteLine("{0}\t\t{1}\t{2}\t{3}", index, pc.Name, pc.Brand, pc.Price);
                index++;
            }
        }

        private void Buy()
        {
            try
            {
                Console.Write("请选择电脑编号:");
                int pcId = int.Parse(Console.ReadLine());
                if (pcId < 0 || pcId >= pcList.Count)
                {
                    Console.WriteLine("啥呀，这都会输错!");
                    return;
                }
                Computer pc = pcList[pcId];//获得购买的电脑
                Console.WriteLine("购买的电脑信息如下:");
                Console.WriteLine("型号:{0}", pc.Name);
                Console.WriteLine("品牌:{0}", pc.Brand);
                Console.WriteLine("价格:{0}", pc.Price);
                //将已经购买的电脑删除
                pcList.Remove(pc);
            }
            catch (Exception)
            {
                Console.WriteLine("傻呀,请输入数字");
            }
        }


        /// <summary>
        /// 销售开始招呼你
        /// </summary>
        private void Hello()
        {
            Console.WriteLine("--请选择您的操作:");
            foreach (string key in menuList.Keys)
            {
                Console.WriteLine("{0}:{1}", key, menuList[key]);
            }
            string selectMenu = Console.ReadLine().ToLower();//获取用户的选择项
            if (menuList.ContainsKey(selectMenu))//判断是否包含项
            {
                switch (selectMenu)
                {
                    case "show":
                        ShowAllPc();
                        Hello();
                        break;
                    case "buy":
                        ShowAllPc();//先显示所有的电脑
                        Buy();
                        Hello();
                        break;
                    case "exit":
                        Console.WriteLine("欢迎再次光临");
                        break;
                }
            }
            else
            {
                Console.WriteLine("输出错误！");
                Hello();
            }
        }
    }
}
